package org.lightweightimageviewer.liv;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
/**
 * This class Transforms images too large for the screen.
 * @author Steven Boscarine
 *
 */
public class ImageResizer {
    private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ImageResizer.class);
    ImageResizer(Dimension viewableArea){
    	VIEWABLE_AREA = viewableArea;
    	DESIRED_HEIGHT = (int)Math.round(SCALE * VIEWABLE_AREA.getHeight());
    	DESIRED_WIDTH = (int)Math.round(SCALE * VIEWABLE_AREA.getWidth());
    	ASPECT_RATIO = VIEWABLE_AREA.getWidth()/VIEWABLE_AREA.getHeight();
    }
    /**	The scale factor     */
    private static final double SCALE = UserPreferences.SCALE;
    /**	Resolution of user's computer	 */
    private static final Dimension SCREEN_RESOLUTION = Toolkit.getDefaultToolkit().getScreenSize();
    
    private final Dimension VIEWABLE_AREA;
    /**	Our target height.  Value rounded to long for debugging.     */
    private final int DESIRED_HEIGHT;
    /**	Our target width.  Value rounded to long for debugging.     */
    private final int DESIRED_WIDTH;
    private final double ASPECT_RATIO;
    
    final BufferedImage resize(BufferedImage img){
        final int h = img.getHeight();
        final int w = img.getWidth();
    	if(h > DESIRED_HEIGHT || w > DESIRED_WIDTH){
        	double aspectRatio = (double)w/(double)h;
        	if(aspectRatio > ASPECT_RATIO){
                int h2 = (int)((1/aspectRatio)*DESIRED_WIDTH);
                int w2 = DESIRED_WIDTH;
                logger.debug("h=" + h + " w=" + w + " This image is too wide.  It should be " + h2 + " tall and " + w2 + " wide");
                img = resize(img, w2, h2);
            }else{
                int h2 = DESIRED_HEIGHT;
                int w2 = (int)(aspectRatio*DESIRED_HEIGHT);
                logger.debug("h=" + h + " w=" + w + " This image is too tall.  It should be " + h2 + " tall and " + w2 + " wide");
                img = resize(img, w2, h2);
            }
        }
        return img;
    }

    final BufferedImage resize(BufferedImage img, Dimension d){
        final int h = (int)d.getHeight();
        final int w = (int)d.getWidth();
        return resize(img, w, h);
    }

    private static final BufferedImage resize(BufferedImage img, int newW, int newH) {
		BufferedImage tmp = new BufferedImage(newW, newH, img.getType());
		Graphics2D g = tmp.createGraphics();
		//g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		//g.drawImage(img, 0, 0, w2, h2, 0, 0, w, h, null);
		g.drawImage(img, 0, 0, newW, newH, null);
		g.dispose();
		return tmp;
    }
    
    private static ImageResizer imageResizer;
    private static boolean reinit = false;		//stupid hack
    static ImageResizer getInstance(){
    	if(imageResizer == null){
    		init(SCREEN_RESOLUTION);
    		reinit = true;
    	}
    	
    	return imageResizer;
    }
    
    static void init(Dimension dim){
    	if(imageResizer == null || reinit){
    		ImageResizer.imageResizer = new ImageResizer(dim);
    		reinit=false;
    	}
    }
}